#include "StdAfx.h"
#include "WriteLogFile.h"
#include <io.h>
#include <direct.h>
#include "..\Function\FilyFunction.h"

std::string CWriteLogFile::m_SavePath = "C:\\";
CWriteLogFile::CWriteLogFile()
{
}

CWriteLogFile::~CWriteLogFile()
{
}



// SetSavePath
void CWriteLogFile::SetSavePath(const std::string& path)
{
	m_SavePath = path;
}
void CWriteLogFile::SetSavePath(const std::wstring& path)
{
	char tmp[255];
	memset(tmp,0,sizeof(tmp));
	CFilyFunction::WCharToMByte(path.c_str(),tmp,255);
	m_SavePath = tmp;
}

//CreateSavePath
bool CWriteLogFile::CreateSavePath(const std::string& path)
{
	const char *fileName=path.c_str(),*tag;
	for(tag=fileName;*tag;tag++)
	{
		if (*tag=='\\')
		{
			char buf[1000],path[1000];
			strcpy(buf,fileName);
			buf[strlen(fileName)-strlen(tag)+1]=NULL;
			strcpy(path,buf);
			if (access(path,6)==-1)
			{
				mkdir(path);
			}
		}
	}
	return true;
}


// Write File Date
void CWriteLogFile::WriteFileDate(const std::string& data)
{	
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep);
	char orderdata[255];
	memset(orderdata,0,sizeof(orderdata));
	sprintf_s(orderdata,"%s\\%d\\%02d\\%02d\\%02d_LogFile.txt",m_SavePath.c_str(),1900+p->tm_year,
		1+p->tm_mon,p->tm_mday,p->tm_hour);
	std::string strfilename = orderdata;
	if(CreateSavePath(strfilename))
	{
		p=localtime(&timep);
		memset(orderdata,0,sizeof(orderdata));
		sprintf_s(orderdata,"%02d:%02d   ",p->tm_sec,p->tm_min);
		std::string strdata = orderdata + data;
		WriteFile(strfilename,strdata);
	}
}
void CWriteLogFile::WriteFileDate(const char * data)
{	
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep);
	char orderdata[255];
	memset(orderdata,0,sizeof(orderdata));
	sprintf_s(orderdata,"%s\\%d\\%02d\\%02d\\%02d_LogFile.txt",m_SavePath.c_str(),1900+p->tm_year,
		1+p->tm_mon,p->tm_mday,p->tm_hour);
	std::string strfilename = orderdata;
	if(CreateSavePath(strfilename))
	{
		p=localtime(&timep);
		memset(orderdata,0,sizeof(orderdata));
		sprintf_s(orderdata,"%02d:%02d   ",p->tm_sec,p->tm_min);
		std::string strdata = orderdata;
		strdata += data;
		WriteFile(strfilename,strdata);
	}
}

void CWriteLogFile::WriteFile(const std::string& data)
{
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep);
	char orderdata[255];
	memset(orderdata,0,sizeof(orderdata));
	sprintf_s(orderdata,"%s\\%d_%02d_%02d_LogFile.txt",m_SavePath.c_str(),1900+p->tm_year,
		1+p->tm_mon,p->tm_mday);
	std::string strfilename = orderdata;
	if(CreateSavePath(strfilename))
	{
		p=localtime(&timep);
		memset(orderdata,0,sizeof(orderdata));
		sprintf_s(orderdata,"%02d:%02d:%02d   ",p->tm_hour,p->tm_sec,p->tm_min);
		std::string strdata = orderdata + data;
		WriteFile(strfilename,data);
	}
}


// WriteFile
void CWriteLogFile::WriteFile(const std::string& filename, const std::string& data)
{	
	FILE * pfile;
	pfile = fopen(filename.c_str(),"a+");
	if (pfile!=NULL)
	{
		fputs (data.c_str(),pfile);
		fputs ("\n",pfile);
		fclose (pfile);
	}
}

// Write File Date
void CWriteLogFile::WriteFileDate(const char * data,int length)
{	
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep);
	char orderdata[255];
	memset(orderdata,0,sizeof(orderdata));
	sprintf_s(orderdata,"%s\\%d\\%02d\\%02d\\%02d_LogFile.txt",m_SavePath.c_str(),1900+p->tm_year,
		1+p->tm_mon,p->tm_mday,p->tm_hour);
	std::string strfilename = orderdata;
	if(CreateSavePath(strfilename))
	{
		p=localtime(&timep);
		memset(orderdata,0,sizeof(orderdata));
		sprintf_s(orderdata,"%02d:%02d   ",p->tm_sec,p->tm_min);
		WriteFile(strfilename,orderdata,data,length);
	}
}

void CWriteLogFile::WriteFile(const char * data,int length)
{
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep);
	char orderdata[255];
	memset(orderdata,0,sizeof(orderdata));
	sprintf_s(orderdata,"%s\\%d_%02d_%02d_LogFile.txt",m_SavePath.c_str(),1900+p->tm_year,
		1+p->tm_mon,p->tm_mday);
	std::string strfilename = orderdata;
	if(CreateSavePath(strfilename))
	{
		p=localtime(&timep);
		memset(orderdata,0,sizeof(orderdata));
		sprintf_s(orderdata,"%02d:%02d:%02d   ",p->tm_hour,p->tm_sec,p->tm_min);
		WriteFile(strfilename,orderdata,data,length);
	}
}


// WriteFile
void CWriteLogFile::WriteFile(const std::string& filename,char * time, const char * data,int length)
{	
	FILE * pfile;
	pfile = fopen(filename.c_str(),"a+");
	if (pfile!=NULL)
	{
		fputs (time,pfile);
		fputs (data,pfile);
		fwrite  (data,1,length,pfile);
		fputs ("\n",pfile);
		fclose (pfile);
	}
}



